﻿using Hangfire;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.EquipmentProcessDomain;
using Zocono.WCS.Domain.JobExcutorDomain;
using Zocono.WCS.Domain.WcsGetWmsJobDomain;

namespace Zocono.WCS.HangfireHost.TaskJobs
{
    /// <summary>
    /// 获取数据库数据存到缓存
    /// </summary>
    public class RepeatGetTableDataJobInvoking
    {

        private readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly TaskLock _taskLock;
        private readonly ILogUtil _logUtil;
        public RepeatGetTableDataJobInvoking(IServiceScopeFactory serviceScopeFactory, TaskLock taskLock, ILogUtil logUtil)
        {
            _serviceScopeFactory = serviceScopeFactory;
            _taskLock = taskLock;
            _logUtil = logUtil;
        }


        /// <summary>
        /// 使用独立的线程执行获取监控数据job
        /// </summary>
        /// <returns></returns>
        public void HangfireJob()
        {
            if (_taskLock.DataToMenTaskLockInvoking)
            {
                return;
            }
            try
            {
                _taskLock.DataToMenTaskLockInvoking = true;
                DateTime dt = DateTime.Now;
                bool isFinish = false;
                Thread thread = new Thread(new ThreadStart(async () =>
                {
                    try
                    {
                        using (var scope = _serviceScopeFactory.CreateScope())
                        {
                            var _RepeatGetTableDataJob = scope.ServiceProvider.GetRequiredService<RepeatGetTableDataJob>();
                            await _RepeatGetTableDataJob.DoJob();
                        }
                    } catch (Exception ex)
                    {
                        _logUtil.Error(ex.ToString());
                    }
                    isFinish = true;
                }));
                thread.Start();
                while (true)
                {
                    if (isFinish)
                    {
                        _logUtil.Debug($"获取监控数据job正常结束，job执行时长:{DateTime.Now.Subtract(dt).TotalMilliseconds}");
                        break;
                    }
                    if (DateTime.Now.Subtract(dt).TotalSeconds > 10)
                    {
                        _logUtil.Fatal($"获取监控数据job超时，强制中断当前线程，job执行开始时间:{dt:YYYY-MM-dd HH:mm:ss}");
                        thread.IsBackground = true;
                        thread.Interrupt();
                        break;
                    }
                    Thread.Sleep(100);
                }

            }
            catch (Exception ex) {
                _logUtil.Fatal(ex.ToString());
            }
            finally
            {
                _taskLock.DataToMenTaskLockInvoking = false;
            }

        }

    }
}
